<?php

namespace App\Http\Controllers\Web;

use App\Http\Service\User;
use App\Http\Service\Msg;
use App\Http\Service\Room;
use App\Http\Service\Chat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ChatController extends Controller{
    /**
     * 展示页面
     */
    public function index(Request $request, User $user){
        $eid  = 1;
        $uid  = $request['uid'];
        //获取聊天时的信息
        $info = $this->getChatInfo($uid);

        $chatRoomId = $eid;//聊天室id

        return view("chat.index",compact('info','chatRoomId'));
    }
    /**
     * 展示页面
     */
    public function pc(Request $request, User $user){
        $eid  = 1;
        $uid  = $request['uid'];
        //获取聊天时的信息
        $info = $this->getChatInfo($uid);

        $chatRoomId = $eid;

        return view("chat.pc",compact('info','chatRoomId'));
    }

    /**
     * 聊天室发送消息
     */
    public function roomMsg(Request $request,Msg $msg){
        $res = $msg->publish();
        dd($res);
    }

    /**
     * 房间操作
     */
    public function room(Room $room){
        $eid = 1;
        //创建房间
//        $info = $room->userQuery($eid,10);      //查询聊天室的成员
        $info = $room->userExist($eid,1);  //查询用户是否在聊天室中
        dd(json_decode($info,true));
    }

    /**
     * 用户操作
     */
    public function user(user $user){
        $eid = 1;
        $uid = 1;
        $info = $user->forbidUser($eid,$uid,10);
//        $info = $user->forbidQuery($eid);
        dd($info);
    }


    /**
     * 获取用户聊天时的信息
     */
    public function getChatInfo($uid){
        $nickname   = "用户" . $uid;
        $headimgurl = "/web/img/face" . $uid . ".jpg";
        $userData = [
            'uid'      => $uid,          // 用户id
            'nickname' => $nickname,     // 用户名
            'avatar'   => $headimgurl,
        ];

        $chatObj  = new Chat;
        $chatInfo = $chatObj->getTokenAndKey($userData);
        $return   = array_merge($userData,$chatInfo);
        return $return;
    }
}

